package com.rc.activity.sync.util;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;

import com.rc.activity.dao.DaoSession;
import com.rc.activity.dao.tbl_Report;
import com.rc.activity.dao.tbl_ReportDao;
import com.rc.net.NetSupport;

public class ReportSync {

	private Context context;
	private SQLiteDatabase db;
	private DaoSession daoSession;
	public String query;
	private tbl_ReportDao daoReport;
	public Cursor cursor;
	public static String[] keys = { "status", "ReportID", "messages" };

	public ReportSync(Context context, SQLiteDatabase db, DaoSession daoSession) {
		this.context = context;
		this.daoSession = daoSession;
		this.db = db;
		daoReport = this.daoSession.getTbl_ReportDao();
	}

	/**
	 * Delete multi report
	 */
	public void deleteReport() {
		new callDeleteReport().execute("");
	}

	/**
	 * Insert report
	 */
	public void insertReport() {
		new callInsertReport().execute("");
	}

	/**
	 * Update report
	 */
	public void UpdateReport() {
		new callUpdateReport().execute("");
	}

	/**
	 * call multidelteReport
	 */
	private class callDeleteReport extends
			AsyncTask<String, Void, ArrayList<Long>> {

		public callDeleteReport() {
			super();
		}

		@Override
		protected ArrayList<Long> doInBackground(String... params) {
			// The string name is query, providing that "isDelete" = 1
			query = tbl_ReportDao.Properties.IsDelete.columnName + " = '1'";
			ArrayList<tbl_Report> arrReport;
			ArrayList<Long> arrReportID = new ArrayList<Long>();

			// get data in sqlite
			cursor = db.query(daoReport.getTablename(),
					daoReport.getAllColumns(), query, null, null, null, null);
			arrReport = new ArrayList<tbl_Report>();

			// get ArrayList Report by cursor
			arrReport = tbl_Report.getObjectbyCursor(cursor);
			if (arrReport.size() > 0) {
				for (tbl_Report objReport : arrReport) {
					// get ID Report
					arrReportID.add(objReport.getReportID());
				}
				// query server
				if (NetSupport.deleteReport(arrReportID, context)) {
					return arrReportID;
				}
			}
			return null;

		}

		protected void onPostExecute(ArrayList<Long> arrReportID) {
			insertReport();
		}
	}

	/**
	 * call multiInsertReport
	 */
	private class callInsertReport extends AsyncTask<String, Void, Void> {

		@Override
		protected Void doInBackground(String... params) {
			// The string name is query, providing that "isDelete" = 1
			query = tbl_ReportDao.Properties.IsCreate.columnName + " = '1'";
			ArrayList<tbl_Report> arrReport;

			// get data in sqlite
			cursor = db.query(daoReport.getTablename(),
					daoReport.getAllColumns(), query, null, null, null, null);
			arrReport = new ArrayList<tbl_Report>();

			// get ArrayList Report by cursor
			arrReport = tbl_Report.getObjectbyCursor(cursor);
			if (arrReport.size() > 0) {

				// query server
				NetSupport.insertReports(arrReport, context);
			}
			return null;

		}

		@Override
		protected void onPostExecute(Void result) {
			deleteReport();
		}
	}

	/**
	 * call multiInsertReport
	 */
	private class callUpdateReport extends
			AsyncTask<String, Void, ArrayList<tbl_Report>> {

		@Override
		protected ArrayList<tbl_Report> doInBackground(String... params) {
			// The string name is query, providing that "isDelete" = 1
			query = tbl_ReportDao.Properties.IsUpdate.columnName + " = '1'";
			ArrayList<tbl_Report> arrReport;

			// get data in sqlite
			cursor = db.query(daoReport.getTablename(),
					daoReport.getAllColumns(), query, null, null, null, null);
			arrReport = new ArrayList<tbl_Report>();

			// get ArrayList Report by cursor
			arrReport = tbl_Report.getObjectbyCursor(cursor);
			if (arrReport.size() > 0) {

				// query server
				if (NetSupport.updateReports(arrReport, context)) {
					return arrReport;
				}
			}
			return null;

		}

		protected void onPostExecute(ArrayList<tbl_Report> arrReportID) {
		}
	}
}
